<?php

using("njoy.system.*");
using("njoy.data.persistence.*");

class SansureHayir_comment extends View
{
	public $page_name;
	public $comment_start;
	public $commentList;
	public $pagesize;
	public $has_paging;
	public $comment;
	public $name;
	public $url;

	public function Load()
	{
		$q = new Query();

		//Coment List
		
		$comment_total = $q->Select("Count(CategoryId) as RowSize")->From("sansurehayir_comment")->Where("CategoryId")->
				EqualTo($this->Page->Parameters["categoryid"])->AndWith("IsPublished")->EqualTo("1")->Execute();

		$rowsize = 0;
		foreach ($comment_total as $comment)
		{
			$rowsize = $comment->rowsize;
			break;
		}

		$this->pagesize = floor($rowsize / 10 + (($rowsize % 10) > 0 ? 1 : 0));
		$this->has_paging = ($this->pagesize > 1);

		if (isset($_POST["curr_page"]))
		{
			$pageNo = $_POST["curr_page"];
		}
		else
		{
			$pageNo = $this->Page->Parameters["page"];
		}

		if ($pageNo == 0)
		{
			$pageNo = 1;
		}

		if (isset($_POST["prev"]))
		{
			$pageNo--;
		}

		if (isset($_POST["next"]))
		{
			$pageNo++;
		}

		if ($pageNo <= 0)
		{
			$pageNo = 1;
		}

		if ($pageNo > $this->pagesize)
		{
			$pageNo = $this->pagesize;
		}

		$this->curr_page = $pageNo;

		$this->comment_start = (($pageNo - 1) * 10) + 1;

		$comments =	$q->Select()->From("sansurehayir_comment")->Where("CategoryId")->
				EqualTo($this->Page->Parameters["categoryid"])->AndWith("IsPublished")->EqualTo("1")->OrderBy()->Asc("CreatedOn")->
				Paging(10, $pageNo)->Execute();

		$commentList = Array();
		foreach($comments as $c)
		{
			$url = str_replace("http://", "", $c->url);
			if (strlen($url)) {
				$owner = "<a href='http://$url'>{$c->fullname}</a>";
			} else {
				$owner = "<b>{$c->fullname}</b>";
			}

			$this->commentList[$c->id] = Array("owner"=>$owner, "created_on"=>$c->createdon, "comment"=>str_replace("\n", "<br/>", $c->description));
		}
		

		$this->page_name = $this->Page->SiteMapPage->Name;

		$this->comment = "yorumunuz";
		$this->name = "anonim";
		$this->url = "http://";

		// Burada kaydet
		if (isset($_POST["send"]))
		{
			$this->comment = securedata($_POST["comment"]);
			$this->name = securedata($_POST["name"]);
			$this->url = securedata($_POST["url"]);

			if (strlen($this->name) == 0 || strlen($this->comment) == 0)
			{
				$this->notification = "<span class='notiferr'> Lütfen adınız ve/veya yorumunuzu bizimle paylaşın! </span><br/><br/>";
				return;
			}

			$q->Insert()->Into("sansurehayir_comment")->
					Fields("CategoryId", "RemoteAddr", "Fullname", "Url", "Description", "CreatedOn", "IsPublished")->
					Values($this->Page->Parameters["categoryid"], $_SERVER["REMOTE_ADDR"], 
						$this->name, $this->url, $this->comment, date("Y-m-d H:i:s"), "0")->
					Execute();

			$this->notification = "<span class='notifi'> Katkılımınız için teşekkür ederiz ! </span><br/><br/>";

			$this->comment = $_POST["comment"];
			$this->name = $_POST["name"];
			$this->url = $_POST["url"];

		}

	
	}
}

?>
